layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'article_list',
		form_filter = 'article_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: article_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'sort',
			_b: 'asc'
		},
		cols: [article_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#articleToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'sort',
			type: 'asc'
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch(obj.event) {
			case 'addArticle':
				create_tab($(this));
				break;
			case 'delArticle':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if(checkStatus.data.length == 0) {
					layer.msg('请选择要删除的数据', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var article_id_arr = new Array();
				layer.confirm('确定删除选中数据吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						article_id_arr.push(obj.article_id);
					});
					send_info(article_delete_url, {
						article_id: article_id_arr
					}, function(data) {
						if(data.code == 0) {
							tr.map(function(obj, i) {
								$(obj.selector).remove();
							})
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if($('table[lay-filter="recycle_article_list"]').length > 0) {
								table.reload('recycle_article_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	// select 查询
	form.on('select(artclass_id)', function(data) {
		var value = data.value;
		var where = {};
		where['artclass_id'] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	})
	var edit_article = function(field, value, article_id) {
		var post = '{"article_id":"' + article_id + '","' + field + '":"' + value + '"}';
		var data = JSON.parse(post);
		send_info(article_save_url, data, function(data) {
			if(data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			article_id = obj.data.article_id,
			tr = obj.tr;
		if(value == ''){
			layer.msg('不能为空',{icon:2});
			tr.find('td[data-field="'+field+'"]').html('<div class="layui-table-cell laytable-cell-'+tr.find('td[data-field="'+field+'"]').attr('data-key')+'">'+tr.find('td[data-field="'+field+'"]').attr('data-val')+'</div>');
			return false;
		}
		edit_article(field, value, article_id);
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var article_id = data.article_id;
		switch(e) {
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(article_delete_url, {
						article_id: article_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if($('table[lay-filter="recycle_article_list"]').length > 0) {
								table.reload('recycle_article_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(article_one_url, {
					article_id: article_id
				}, function(data) {
					delete data.data.article_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	})
})